<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Event/classes/PhpFox_ComponentRSVP.class.php, $Author:  $ */


/** Component for building RSVP form
 * @version $Id: PhpFox_ComponentRSVP.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.event
 *
 */
class PhpFox_Mod_Event_ComponentRSVP extends BaseComponent
{
    function process()
    {
        $oEventInviteItem = $this->_oModule->getItem('EventInvite');
        /* @var $oEventInviteItem PhpFox_Mod_Event_ItemEventInvite */
        $oEventItem = $this->_oModule->getItem('Event');
        /* @var $oEventItem PhpFox_Mod_Event_ItemEvent */
        $oEventService = $this->_oModule->getService('Event');
        /* @var $oEventService PhpFox_Mod_Event_ServiceEvent */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oBlockService = &App::getModuleService('Account', 'Block');
        /* @var $oBlockService PhpFox_Mod_Account_ServiceBlock */
        $oReq = &App::getRequest();
        $oTpl = $this->_oModule->getTemplate();
        App::loadClass('Text');
        $oTxt = &new Text();

        $oSecurityService->checkPerm('events', array('full', 'read_only'));
        $oSecurityService->checkAccess('events');
        $iEventId = $oReq->getInt('id');
        if (!$iEventId)
        {
            App::gotoUrl('public.event.main');
        }
        if (!$oEventService->isEventCanViewed($iEventId))
        {
            App::gotoUrl('public.event.main');
        }
        if (!$oEventItem->load($iEventId))
        {
            App::gotoUrl('public.event.main');
        }
        $oCurrentUser = $oSecurityService->getCurrentUser();
        $sUser = (isset($oCurrentUser) ? $oCurrentUser->aData['user'] : '');
        if (!$sUser)
        {
            return '';
        }
        if ($oReq->getAction() == 'rsvp')
        {
            $oSecurityService->checkPerm('events', array('full'));
            $sOwnerName = $oEventItem->aData['user'];
            $oOwner = $oSecurityService->getUserByName($sOwnerName);
            if ($oBlockService->isBlocked($oOwner, $oCurrentUser))
            {
                $this->addErrors(App::format('Mod_Event.error.blocked_rsvp'));
            }
            if (count($this->getErrors()))
            {
                App::gotoUrl('public.event.view', array('id' => $iEventId), $this->getErrors(), true, 'rsvp');
            }
            $aInvites = array(1, 2, 3);
            $iInvite = $oReq->getInt('rsvp');
            $iInviteId = $oEventInviteItem->oDb->getField('SELECT id '.
            										   	 ' FROM '.App::getT('event_invite').' '.
            										   	 ' WHERE event_id = '.$iEventId.' '.
            										   	 '   AND user = "'.$sUser.'" '.
                                                         '   AND type = 0');
            if (!in_array($iInvite, $aInvites))
            {
                App::gotoUrl('public.event.view', array('id' => $iEventId));
            }
            if ($iInviteId)
            {
                $oEventInviteItem->load($iInviteId);
            	$oEventInviteItem->aData['text']   = $oTxt->preParseText($oReq->get('comment'));
            	$oEventInviteItem->aData['invite'] = $iInvite;
                $oEventInviteItem->update(array('text', 'invite'));
            }
            else
            {
                $oEventInviteItem->aData['event_id'] = $iEventId;
            	$oEventInviteItem->aData['user']     = $sUser;
            	$oEventInviteItem->aData['invite']   = $iInvite;
            	$oEventInviteItem->aData['text']     = $oTxt->preParseText($oReq->get('comment'));
                $oEventInviteItem->aData['time']     = time();
                $oEventInviteItem->insert();
            }
            App::gotoUrl('public.event.view', array('id' => $iEventId));
        }
        $aItem = array();
        $iCnt = 0;
        if ($sUser)
        {
            $aCond = array();
            $aCond['event_id'] = '= '.$iEventId;
            $aCond['user']     = '= "'.$sUser.'"';
            $aCond['invite']   = '<> 0';
            list($aItem, $iCnt) = $oEventInviteItem->getList($aCond, 1, 1);
        }

        $oTpl->assign(array(
        	'aItem'    => ($iCnt) ? $aItem[0] : array(),
        	'iCnt'     => $iCnt,
        	'iEventId' => $iEventId,
        	'bChange'  => $oReq->getInt('change'),
        ));
        return $oTpl->fetch('RSVP.html');
    }
}
?>